package com.spring.mvc.session.client;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.spring.mvc.session.RemoteSessionService;
import com.spring.mvc.session.SessionMsgBean;
import com.spring.mvc.session.server.RemoteSessionServiceImpl;

public class SessionServiceImpl implements SessionService {
	
	private String domainName="";
	private int hashIndex;

	public SessionServiceImpl() {
	}

	public void createNewSession(String sessionID, String remoteAddr) {
		SessionMsgBean bean=new SessionMsgBean();
		bean.setSessionID(sessionID);
		bean.setRemoteAddr(remoteAddr);
		
		RemoteSessionService service=new RemoteSessionServiceImpl();
		service.findSession(bean);
		
		bean=null;
		service=null;
	}

	public Object getAttribute(String id, String key, String groupId) {
		SessionMsgBean bean=new SessionMsgBean();
		
		bean.setSessionID(id);
		bean.setAttributeKey(key);
		
		/*********************************远程获取Session********************/
		Object object_=null;
		if(true){//如果从远程获取对象成功
			byte[] data_=(byte[])bean.getValue();
			if(data_!=null){
				ByteArrayInputStream bin=null;
				ObjectInputStream in=null;
				try {
					bin=new ByteArrayInputStream(data_);
					in=new ObjectInputStream(bin);
					object_=in.readObject();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} finally{
					try {
						if(bin!=null){
							bin.close();
							bin=null;
						}
						if(in!=null){
							in.close();
							in=null;
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return object_;
	}

	public String getDomainName() {
		return this.domainName;
	}

	public int getHashIndex() {
		return this.hashIndex;
	}

	public void invalidate(String param1, String param2) {
		// TODO Auto-generated method stub
		
	}

	public void removeAttribute(String id, String param2, String param3) {
		// TODO Auto-generated method stub
		
	}

	public void setAttribute(String id, String key, Object value,
			String groupID) {
		SessionMsgBean bean=new SessionMsgBean();
		
		bean.setSessionID(id);
		bean.setAttributeKey(key);
		
		ByteArrayOutputStream o=null;
		ObjectOutputStream out=null;
		byte[] data_=(byte[])null;
		try {
			o=new ByteArrayOutputStream();
			out=new ObjectOutputStream(o);
			out.writeObject(value);
			out.flush();
			out.close();
			out=null;
			data_=o.toByteArray();
			bean.setValue(data_);
			
			/********************************未完*****************************************/
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(o!=null){
					o.close();
					o=null;
				}
				if(out!=null){
					out.close();
					out=null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void setDomainName(String domainName) {
		this.domainName=domainName;
	}

	public void setHashIndex(int hashIndex) {
		this.hashIndex=hashIndex;
	}

	public void setMaxInactiveInterval(String param1, int param2, String param3) {
		// TODO Auto-generated method stub
		
	}

}
